var searchResultApp = angular.module("searchResultApp", []);
searchResultApp.controller("searchResultController", function($scope, $rootScope, $window, scCodeService, Popup, $location,pubFun) {
    document.getElementById("loadingToast1").style.display = "none";
    //当前城市和选择城市
    $scope.selectedCity = JSON.parse(localStorage.getItem("currentCity"));
    $scope.currentCity = JSON.parse(localStorage.getItem("city"));
    $scope.tab1 = false;
    $scope.tab2 = false;
    $scope.tab3 = false;
    $scope.tabClick = false;
    $scope.tab = function(index) {
    	$scope.tabClick = true;
	    if (index == "1") {
	       	$scope.tab1 = $scope.tab1 == false?true:false;
	        $scope.tab2 = false;
	        $scope.tab3 = false;
			$scope.tabClick = $scope.tab1 == false?false:true;        
	    } else if (index == "2") {
	        $scope.tab2 = $scope.tab2 == false?true:false;
	        $scope.tab1 = false;
	        $scope.tab3 = false;
	        $scope.tabClick = $scope.tab2 == false?false:true;
	        
	    } else if (index == "3") {
	        $scope.tab3 = $scope.tab3 == false?true:false;
	        $scope.tab1 = false;
	        $scope.tab2 = false;
	        $scope.tabClick = $scope.tab3 == false?false:true; 	     
	    }
    }
    //条件初始化
    // $scope.dianzhuang = false;
    // $scope.other = false;
    // $scope.dzFlag = false;
    // $scope.otherFlag = false;
    $scope.operator = 1;
    $scope.chargerHide = 1;
    $scope.checkOperator = function(type){
        if(type == 1){
            $scope.operator = 1;
        }else if(type==2){
            $scope.operator = 2;
        }else if(type==3){
            $scope.operator = 3;
        }
         $scope.filtrate();
    }
    $scope.checkCharger = function(type){
        if(type == 1){
            $scope.chargerHide = 1;
        }else if(type==2){
            $scope.chargerHide = 2;
        }else if(type==3){
            $scope.chargerHide = 3;
        }
        $scope.filtrate();
    }
    // $scope.checkDz = function(type){
    //     if(type=='1'){
    //          $scope.dianzhuang = $scope.dianzhuang == false?true:false;           
    //     }else if(type=='2'){
    //     $scope.other = $scope.other == false?true:false;
    //     }
    // }
    //关闭
    $scope.tabClickHide = function(){
    	$scope.tabClick = false;
    	$scope.tab1 = false;
        $scope.tab2 = false;
        $scope.tab3 = false;
        // $scope.dianzhuang = $scope.dzFlag == false?false:true;
        // $scope.other = $scope.otherFlag == false?false:true;
    };
    //搜索结果数据
    $scope.searchResult = JSON.parse(localStorage.getItem("searchResult"));
    //alert(JSON.stringify($scope.searchResult));
    //按距离从近到远排序
    $scope.distance = function(num){
        return parseFloat(num.distance);       
    }
    //根据充电类型筛选
    var directResult = [];
    var alternating = [];
    var operMerchant_1 = [];
    var operMerchant_2 = [];
    var operMerchant_3 = [];
    var operMerchant_4 = [];
    var operDirect = [];
    var operAlternating = [];
    //根据条件分别重组数据
    angular.forEach($scope.searchResult,function(result){
    	if(result.directNum!=="0"){
    		 directResult.push(result);
    	}
    	if(result.alternateNum!=="0"){
    		alternating.push(result);	
    	}
        if(result.operMerchantId=="1201601001"){
            operMerchant_1.push(result);
        }else{
            operMerchant_2.push(result);
        }
        if(result.operMerchantId=="1201601001"&&result.directNum!=="0"){
            operDirect.push(result);
        }
        if(result.operMerchantId=="1201601001"&&result.alternateNum!=="0"){
            operAlternating.push(result);
        }
        if(result.operMerchantId!=="1201601001"&&result.alternateNum!=="0"){
            operMerchant_3.push(result);
        }
        if(result.operMerchantId!=="1201601001"&&result.directNum!=="0"){
            operMerchant_4.push(result);
        }
    })
    //根据条件更新数据
    $scope.filtrate =  function(){
        //如果运营商为all或者充电类型选择all
        if($scope.operator==1||$scope.chargerHide==1){
           $scope.searchResult = JSON.parse(localStorage.getItem("searchResult"));
        }
        //如果运营商选择其他
        if($scope.operator==3){
             $scope.searchResult = operMerchant_2;
        }
        // if($scope.operator==3||($scope.other==true&&$scope.dianzhuang!==true)){
        //      $scope.searchResult = [];
        // }
        //如果运营商为全部和充电类型为直流
        if($scope.operator==1&&$scope.chargerHide==2){
            $scope.searchResult = directResult;
        }
        //如果运营商为全部和充电类型为交流
        if($scope.operator==1&&$scope.chargerHide==3){
            $scope.searchResult = alternating;
        }
        //如果运营商为电桩和充电类型为直流
        if($scope.operator==2&&$scope.chargerHide==2){
            $scope.searchResult = operDirect;
        }
        //如果运营商为电桩和充电类型为交流
        if($scope.operator==2&&$scope.chargerHide==3){
            $scope.searchResult = operAlternating;
        }
        //如果运营商为其他和充电类型为直流
        if($scope.operator==3&&$scope.chargerHide==2){
            $scope.searchResult = operMerchant_4;
        }
        //如果运营商为其他和充电类型为交流
        if($scope.operator==3&&$scope.chargerHide==2){
            $scope.searchResult = operMerchant_3;
        }
        // if($scope.operator==1&&$scope.chargerHide==2&&$scope.other!==true||($scope.operator==1&&$scope.chargerHide==2&&$scope.dianzhuang==true&&$scope.other==true)){
        //     $scope.searchResult = directResult;
        // }
        // if($scope.operator==2&&$scope.chargerHide==2&&$scope.other!==true||($scope.operator==2&&$scope.chargerHide==2&&$scope.dianzhuang==true&&$scope.other==true)){
        //     $scope.searchResult = directResult;
        // }
        // if($scope.operator==1&&$scope.chargerHide==3&&$scope.other!==true||($scope.operator==1&&$scope.chargerHide==3&&$scope.dianzhuang==true&&$scope.other==true)){
        //     $scope.searchResult = alternating;
        // }
        // if($scope.operator==2&&$scope.chargerHide==3&&$scope.other!==true||($scope.operator==2&&$scope.chargerHide==3&&$scope.dianzhuang==true&&$scope.other==true)){
        //     $scope.searchResult = alternating;
        // }
        // $scope.dzFlag = $scope.dianzhuang == true?true:false;
        // $scope.otherFlag = $scope.other == true?true:false;
    	$scope.tabClick = false;
        $scope.tab1 = false;
        $scope.tab2 = false;
        $scope.tab3 = false;
    }
    $scope.goStationDeatil = function(stationId,latitude,longitude,distance){
    	 localStorage.setItem("latitude",latitude);
    	 localStorage.setItem("longitude",longitude);
         document.getElementById("loadingToast1").style.display = "block";
   	     $location.path("/stateDetail/" + stationId +"/"+distance);	 	
    }
    $scope.naviGation = function(latitude,longitude,address){
		window.location.href = "https://map.qq.com/m/mqq/nav/eword="+address+"&epointx="+longitude+"&epointy="+latitude+"?referer=myapp&key=P44BZ-LANRX-D6I46-ZTXJL-OHJT2-LKBR3"    	
    }
})
